/**
 * Created by qunxing.du on 2016/8/10.
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.zj.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

import com.hw.one.collect.config.ConditionConfig;
import com.hw.one.collect.zj.config.ZjConfig;
import org.apache.log4j.Logger;



/**
 * 中建7局数据推送客户端
 */
public class ZjClient {
	//public final String SERVER_IP = "10.0.16.12";
	//public final int SERVER_PORT = 60006;
//	public final String SERVER_IP = "123.15.58.210";
//	public final int SERVER_PORT = 9123;
	public final String SERVER_IP = ConditionConfig.get("SERVER_IP", "zjfivemin");
	public final int SERVER_PORT = Integer.parseInt(ConditionConfig.get("SERVER_PORT", "zjfivemin"));
	private Logger log = Logger.getLogger(this.getClass());
	public Object send(List<String> o) {
		if(o==null)
			return null;
		//System.out.println("send date:" + o);
		log.info("send date:" + o);	
		String ret = "";
		try {

			for (int i = 0; i < 5; i++) {
				Socket socket = new Socket(SERVER_IP, SERVER_PORT);
				// 发送命令
				OutputStream socketOut = socket.getOutputStream();
				InputStream br = null;
				for(String command : o)
					socketOut.write(command.getBytes("UTF-8"));

				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// 接收服务器的反馈
				br = socket.getInputStream();
				byte[] v = new byte[100];
				if (br.available() != 0) {
					br.read(v);
					ret = new String(v, "UTF-8").trim();
				}
				if (ret != null & ret.length() != 0) {
					log.info("Server return : " + ret);
					socketOut.close();
					br.close();
					socket.close();
					break;
				}
				socketOut.close();
				br.close();
				socket.close();

			}

		} catch (IOException e) {
			// e.printStackTrace();
			System.err.println("同服务器交互时" + e.getMessage());
		} finally {

		}
		return null;

	}

}
